﻿function Linear(AQIhigh, AQIlow, Conchigh, Conclow, Concentration) {
    var linear;
    var Conc = parseFloat(Concentration);
    var a;
    a = ((Conc - Conclow) / (Conchigh - Conclow)) * (AQIhigh - AQIlow) + AQIlow;
    linear = Math.round(a);
    return linear;
}


function AQICO(Concentration) {
    var Conc = parseFloat(Concentration);
    var c;
    var AQI;
    c = (Math.floor(10 * Conc)) / 10;
    if (c >= 0 && c < 4.5) {
        AQI = Linear(50, 0, 4.4, 0, c);
    }
    else if (c >= 4.5 && c < 9.5) {
        AQI = Linear(100, 51, 9.4, 4.5, c);
    }
    else if (c >= 9.5 && c < 12.5) {
        AQI = Linear(150, 101, 12.4, 9.5, c);
    }
    else if (c >= 12.5 && c < 15.5) {
        AQI = Linear(200, 151, 15.4, 12.5, c);
    }
    else if (c >= 15.5 && c < 30.5) {
        AQI = Linear(300, 201, 30.4, 15.5, c);
    }
    else if (c >= 30.5 && c < 40.5) {
        AQI = Linear(400, 301, 40.4, 30.5, c);
    }
    else if (c >= 40.5 && c < 50.5) {
        AQI = Linear(500, 401, 50.4, 40.5, c);
    }
    else {
        AQI = "Out of Range";
    }
    return AQI;
}
function AQINO2(Concentration) {
    var Conc = parseFloat(Concentration);
    var c;
    var AQI;
    c = (Math.floor(Conc)) / 1000;
    if (c >= 0 && c < .054) {
        AQI = Linear(50, 0, .053, 0, c);
    }
    else if (c >= .054 && c < .101) {
        AQI = Linear(100, 51, .100, .054, c);
    }
    else if (c >= .101 && c < .361) {
        AQI = Linear(150, 101, .360, .101, c);
    }
    else if (c >= .361 && c < .650) {
        AQI = Linear(200, 151, .649, .361, c);
    }
    else if (c >= .650 && c < 1.250) {
        AQI = Linear(300, 201, 1.249, .650, c);
    }
    else if (c >= 1.250 && c < 1.650) {
        AQI = Linear(400, 301, 1.649, 1.250, c);
    }
    else if (c >= 1.650 && c <= 2.049) {
        AQI = Linear(500, 401, 2.049, 1.650, c);
    }
    else {
        AQI = "Out of Range";
    }
    return AQI;
}

function AQICategory(AQIndex) {
    var AQI = parseFloat(AQIndex)
    var AQICategory;
    if (AQI <= 50) {
        AQICategory = "Good";
    }
    else if (AQI > 50 && AQI <= 100) {
        AQICategory = "Moderate";
    }
    else if (AQI > 100 && AQI <= 150) {
        AQICategory = "Unhealthy for Sensitive Groups";
    }
    else if (AQI > 150 && AQI <= 200) {
        AQICategory = "Unhealthy";
    }
    else if (AQI > 200 && AQI <= 300) {
        AQICategory = "Very Unhealthy";
    }
    else if (AQI > 300 && AQI <= 400) {
        AQICategory = "Hazardous";
    }
    else if (AQI > 400 && AQI <= 500) {
        AQICategory = "Hazardous";
    }
    else {
        AQICategory = "Out of Range";
    }
    return AQICategory;
}

function AQICalcCO(f) {
    co = AQICO(f);
    var COValue = co;
    var COCategory = AQICategory(co);


    var airData = new Array();

    airData["index"] = co;
    airData["category"] = COCategory;
    var TextSensitive = "";
    var Cautionary = "";
    var HealthEffects = "";
    if (COCategory == 'Good') {
        document.form.TextSensitive.value = "People with respiratory disease are the group most at risk.";
    }
    else if (COCategory == 'Moderate') {
        TextSensitive = "People with heart disease are the group most at risk.";
        HealthEffects = "None";
        Cautionary = "None";
    }
    else if (COCategory == 'Unhealthy for Sensitive Groups') {
        TextSensitive = "People with heart disease are the group most at risk.";
        HealthEffects = "Increasing likelihood of reduced exercise tolerance due to increased cardiovascular symptoms, such as chest pain, in people with cardiovascular disease.";
        Cautionary = "People with cardiovascular disease, such as angina, should limit heavy exertion and avoid sources of CO, such as heavy traffic.";
    }
    else if (COCategory == 'Unhealthy') {
        TextSensitive = "People with heart disease are the group most at risk.";
        HealthEffects = "Reduced exercise tolerance due to increased cardiovascular symptoms, such as chest pain, in people with cardiovascular disease.";
        Cautionary = "People with cardiovascular disease, such as angina, should limit moderate exertion and avoid sources of CO, such as heavy traffic.";
    }
    else if (COCategory == 'Very Unhealthy') {
        TextSensitive = "People with heart disease are the group most at risk.";
        HealthEffects = "Significant aggravation of cardiovascular symptoms, such as chest pain, in people with cardiovascular disease.";
        Cautionary = "People with cardiovascular disease, such as angina, should avoid exertion and sources of CO, such as heavy traffic.";
    }
    else if (COCategory == 'Hazardous') {
        TextSensitive = "People with heart disease are the group most at risk.";
        HealthEffects = "Serious aggravation of cardiovascular symptoms, such as chest pain, in people with cardiovascular disease; impairment of strenuous activities in general population.";
        Cautionary = "People with cardiovascular disease, such as angina, should avoid exertion and sources of CO, such as heavy traffic; everyone else should limit heavy exertion.";
    }
    airData["TextSensitive"] = TextSensitive;
    airData["Cautionary"] = Cautionary;
    airData["HealthEffects"] = HealthEffects;
    return airData;
}

function AQICalcNO(f) {

    var airData = new Array();
    var b;
    b = AQINO2(f);
    var NO2Category = AQICategory(b);
    airData["index"] = b;
    airData["category"] = NO2Category;
    var TextSensitive = "";
    var Cautionary = "";
    var HealthEffects = "";
    if (NO2Category == 'Good') {
        TextSensitive = "People with asthma or other respiratory diseases, the elderly, and children are the groups most at risk.";
    }
    else if (NO2Category == 'Moderate') {
        TextSensitive = "People with asthma or other respiratory diseases, the elderly, and children are the groups most at risk.";
        HealthEffects = "Unusually sensitive individuals may experience respiratory symptoms.";
        Cautionary = "Unusually sensitive people should consider reducing prolonged or heavy outdoor exertion.";
    }
    else if (NO2Category == 'Unhealthy for Sensitive Groups') {
        TextSensitive = "People with asthma or other respiratory diseases, the elderly, and children are the groups most at risk.";
        HealthEffects = "Increasing likelihood of respiratory symptoms and breathing discomfort in active children, the elderly, and people with lung disease, such as asthma.";
        Cautionary = "Active children, the elderly, and people with lung disease, such as asthma, should reduce prolonged or heavy outdoor exertion.";
    }
    else if (NO2Category == 'Unhealthy') {
        TextSensitive = "People with asthma or other respiratory diseases, the elderly, and children are the groups most at risk.";
        HealthEffects = "Greater likelihood of respiratory symptoms in active children, the elderly, and people with lung disease, such as asthma; possible respiratory effects in general population.";
        Cautionary = "Active children, the elderly, and people with lung disease, such as asthma, should avoid prolonged or heavy outdoor exertion; everyone else, expecially children, should reduce prolonged or heavy outdoor exertion.";
    }
    else if (NO2Category == 'Very Unhealthy') {
        TextSensitive = "People with asthma or other respiratory diseases, the elderly, and children are the groups most at risk.";
        HealthEffects = "Increasingly severe symptoms and impaired breathing likely in active children, the elderly, and people with lung disease, such as asthma; increasing likelihood of respiratory effects in general population.";
        Cautionary = "Active children, the elderly, and people with lung disease, such as asthma, should avoid all outdoor exertion; everyone else, especially children, should avoid prolonged or heavy outdoor exertion.";
    }
    else if (NO2Category == 'Hazardous') {
        TextSensitive = "People with asthma or other respiratory diseases, the elderly, and children are the groups most at risk.";
        HealthEffects = "Severe respiratory effects and impaired breathing likely in active children, the elderly, and people with lung disease, such as asthma; increasingly severe respiratory effects likely in general population.";
        Cautionary = "Children, the elderly, and people with lung disease, such as asthma, should remain indoors; everyone else, especially children, should avoid outdoor exertion.";
    }
    airData["TextSensitive"] = TextSensitive;
    airData["Cautionary"] = Cautionary;
    airData["HealthEffects"] = HealthEffects;
    return airData;

}
